Number Methods


JavaScript provides several methods to work with numbers. These methods can be used to convert, format, and manipulate numbers.

1. toString()

The toString() method converts a number to a string.

let num = 123;
console.log(num.toString()); // Outputs: "123"

2. toExponential()

The toExponential() method converts a number to exponential notation.

let num = 9.656;
console.log(num.toExponential(2)); // Outputs: "9.66e+0"

3. toFixed()

The toFixed() method formats a number using fixed-point notation.

let num = 9.656;
console.log(num.toFixed(2)); // Outputs: "9.66"

4. toPrecision()

The toPrecision() method formats a number to a specified length.

let num = 9.656;
console.log(num.toPrecision(2)); // Outputs: "9.7"

5. valueOf()

The valueOf() method returns the primitive value of a number.

let num = 123;
console.log(num.valueOf()); // Outputs: 123

6. Number()

The Number() function converts a value to a number.

console.log(Number("123")); // Outputs: 123
console.log(Number("123.45")); // Outputs: 123.45

7. parseFloat()

The parseFloat() function parses a string and returns a floating point number.

console.log(parseFloat("123.45")); // Outputs: 123.45

8. parseInt()

The parseInt() function parses a string and returns an integer.

console.log(parseInt("123.45")); // Outputs: 123